﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;

namespace GoogleNewsProvider
{
    public enum NewsType
    {
        Nation,
        Business,
        Culture,
        Sport,
        Health
    }
    public class GNews
    {
        #region Private members

        string googleNewsNationRSS = @"http://news.google.co.il/news?pz=1&cf=all&ned=iw_il&hl=iw&topic=n&output=rss";
        string googleNewsBusinessRSS = @"http://news.google.co.il/news?pz=1&cf=all&ned=iw_il&hl=iw&topic=b&output=rss";
        string googleNewsCultureRSS = @"http://news.google.co.il/news?pz=1&cf=all&ned=iw_il&hl=iw&topic=e&output=rss";
        string googleNewsSportRSS = @"http://news.google.co.il/news?pz=1&cf=all&ned=iw_il&hl=iw&topic=s&output=rss";
        string googleNewsHealthRSS = @"http://news.google.co.il/news?pz=1&cf=all&ned=iw_il&hl=iw&topic=m&output=rss";

        #endregion

        #region Ctor's
        public GNews()
        {

        }
        #endregion

        #region Public methods

        public List<GNewsItem> ParseGoogleRSSNews(NewsType newsType)
        {
            XElement rssFeed = null;
            switch (newsType)
            {
                case NewsType.Nation:
                    rssFeed = XElement.Load(googleNewsNationRSS);
                    break;
                case NewsType.Business:
                    rssFeed = XElement.Load(googleNewsBusinessRSS);
                    break;
                case NewsType.Culture:
                    rssFeed = XElement.Load(googleNewsCultureRSS);
                    break;
                case NewsType.Sport:
                    rssFeed = XElement.Load(googleNewsSportRSS);
                    break;
                case NewsType.Health:
                    rssFeed = XElement.Load(googleNewsHealthRSS);
                    break;
                default:
                    rssFeed = XElement.Load(googleNewsNationRSS);
                    break;
            }
            IEnumerable<GNewsItem> gNewsItemsList = from item in rssFeed.Elements("channel").Elements("item") select new GNewsItem { Category = item.Element("category").Value , Description = item.Element("description").Value, ItemGUID = item.Element("guid").Value , Link = item.Element("link").Value , Title = item.Element("title").Value , PubDate = item.Element("pubDate").Value};
            string test = string.Empty;
            return gNewsItemsList.ToList();
        }

        #endregion

        #region Helper methods

        public string EscapeXml(string input)
        {
            string output = string.Empty;

            output = input.Replace("&", "&amp;");
            output = output.Replace("'", "&apos;");
            output = output.Replace("\"", "&quot;");
            output = output.Replace(">", "&gt;");
            output = output.Replace("<", "&lt;");

            return output;
        }

        #endregion


    }
}
